package com.blb.article_ms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.article_ms.entity.Comment;
import com.blb.article_ms.entity.po.ArticlePO;
import com.blb.article_ms.entity.vo.ArticleVO;
import com.blb.article_ms.mapper.ArticleMapper;
import com.blb.article_ms.mapper.CommentMapper;
import com.blb.article_ms.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xray
 * @since 2023-06-14
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticlePO> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Page<ArticleVO> getArticlePage(Long current, Long size, String title, String content) {
        return articleMapper.selectArticlePage(new Page<>(current,size),
                "%" + title + "%",
                "%" + content + "%");
    }

    @Transactional
    @Override
    public void removeArticle(Long id) {
        //先删评论，再删文章
        commentMapper.delete(new QueryWrapper<Comment>().lambda().eq(Comment::getArticleId,id));
        articleMapper.deleteById(id);
    }
}
